﻿using System;
using System.Collections.Generic;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Navigation;
using System.Windows.Threading;
using SocialApi.Instagram;
using SocialApi.Instagram.AppSettings;
using SocialApi.TestApp.Models;
using SocialApi.TestApp.Models.Enums;
using SocialApi.TestApp.Views;

namespace SocialApi.TestApp.ViewModels
{
    public class ConsoleContolViewModel : ViewModelBase
    {
        /// <summary>
        /// Доступные методы API
        /// </summary>
        public IList<ApiInfo> Apis { get; private set; }

        /// <summary>
        /// Отображает/скрывает методы вызова API из <see cref="InstagramService"/>
        /// </summary>
        public Visibility MethodVisible
        {
            get { return _methodVisible; }
            set
            {
                _methodVisible = value;
                OnPropertyChanged();
            }
        }
        private Visibility _methodVisible;

        /// <summary>
        /// Отображает/скрывает браузер
        /// </summary>
        public Visibility BrowserVisible
        {
            get { return _browserVisible; }
            set
            {
                _browserVisible = value;
                OnPropertyChanged();
            }
        }
        private Visibility _browserVisible;

        /// <summary>
        /// URL вызова метода
        /// </summary>
        public string Url
        {
            get { return _url; }
            set
            {
                _url = value;
                OnPropertyChanged();
            }
        }
        private string _url;

        private readonly WebBrowser _browser;

        #region API сервисы

        private readonly InstagramService _instagram;

        private readonly SocialType _socialType;

        #endregion

        /// <summary>
        /// Модель консоли вызова методов API
        /// </summary>
        /// <param name="dispatcher">Текущий <see cref="Dispatcher"/></param>
        /// <param name="navigationService">Текущий <see cref="NavigationService"/></param>
        /// <param name="socialType">Тестируемая социальная сеть</param>
        /// <param name="browser">Текущий <see cref="WebBrowser"/></param>
        public ConsoleContolViewModel(Dispatcher dispatcher, NavigationService navigationService, SocialType socialType, WebBrowser browser) 
            : base(dispatcher, navigationService)
        {
            _browser = browser;
            _socialType = socialType;
            _instagram = new InstagramService(new ClientData
            {
                CleintId = "dd2d483a9d424d2da0dc20c3506a6c99",
                ClientSecret = "545397b406f4449c8590b290be35a8aa",
                RedirectUrl = "http://www.google.com",
                Scope = Settings.All
            });
            _browser.Navigated += OnNavigated;
#if NET_4_5 || NET_4_0
            _browser.Navigate(_instagram.AuthUri);
#else
            _browser.Navigate(new Uri(_instagram.AuthUri));
#endif
            MethodVisible = Visibility.Collapsed;
            switch (_socialType)
            {
                case SocialType.Instagram:
                    CreateInstagramApi();
                    break;
            }

        }

        private void OnNavigated(object sender, NavigationEventArgs eventArgs)
        {
            switch (_socialType)
            {
                case SocialType.Instagram:
                    if (eventArgs.Uri.AbsoluteUri.Contains("?code"))
                    {
                        BrowserVisible = Visibility.Collapsed;
                        MethodVisible = Visibility.Visible;
                        NavigationService.Navigate(new IntagramControl(eventArgs.Uri));
                    }
                    break;
            }
        }


        private void CreateInstagramApi()
        {
            Apis = new List<ApiInfo>
            {
                new ApiInfo
                {
                    GroupName = "Пользователи",
                    ApiMethods = new List<string>
                    {
                        "users/{user-id}",
                        "users/self/feed",
                        "users/{user-id}/media/recent",
                        "users/self/media/liked",
                        "users/search"
                    }
                },
                new ApiInfo
                {
                    GroupName = "Relationships",
                    ApiMethods = new List<string>
                    {
                        "users/{user-id}/follows",
                        "users/{user-id}/followed-by",
                        "users/self/requested-by",
                        "users/{user-id}/relationship",
                        "users/{user-id}/relationship (POST)"
                    }
                },
                new ApiInfo
                {
                    GroupName = "Media",
                    ApiMethods = new List<string>
                    {
                        "media/{id}",
                        "media/search",
                        "media/popular"
                    }
                },
                new ApiInfo
                {
                    GroupName = "Comments",
                    ApiMethods = new List<string>
                    {
                        "media/{media-id}/comments",
                        "media/{media-id}/comments (POST)",
                        "media/{media-id}/comments/{comment-id} (DELETE)"
                    }
                },
                new ApiInfo
                {
                    GroupName = "Likes",
                    ApiMethods = new List<string>
                    {
                        "media/{media-id}/likes",
                        "media/{media-id}/likes (POST)",
                        "media/{media-id}/likes (DELETE)"
                    }
                },
                new ApiInfo
                {
                    GroupName = "Tags",
                    ApiMethods = new List<string>
                    {
                        "tags/{tag-name}",
                        "tags/{tag-name}/media/recent",
                        "tags/search"
                    }
                },
                new ApiInfo
                {
                    GroupName = "Locations",
                    ApiMethods = new List<string>
                    {
                        "locations/{location-id}",
                        "locations/{location-id}/media/recent",
                        "locations/search"
                    }
                }
            };
        }
    }
}